package ningbo.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author 宁铂
 * @Date 2021/7/28 19:27
 * @Version 1.0
 */
public class TestRunnable {
    /** 多线程编程方案二 */
    public static void main(String[] args) {

        MyRunnable myRunnable = new MyRunnable();
        /** 把接口实现类对象的引用作为参数传递给Thread类 , 就可以使用Tread的方法 , 包括构造方法*/
//        Thread thread = new Thread(myRunnable);
//        Thread thread1 = new Thread(myRunnable);
//        Thread thread2 = new Thread(myRunnable);
//        Thread thread3 = new Thread(myRunnable,"小花花");
//
//        thread.start();
//        thread1.start();
//        thread2.start();
//        thread3.start();
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        for (int i = 1; i <= 5; i++){
            executorService.execute(myRunnable);
        }

    }
}

/** 自定义多线成类 */
class MyRunnable  implements Runnable{

    @Override
    public void run() {
        for (int i = 1; i <= 10; i++){
            /** Thread.currentThread() : 可作为正在执行的线程队象的引用,为静态方法,可以通过类名之接调用 */
            System.out.println(i+"="+Thread.currentThread().getName());
        }
    }
}